package com.wangsd.common.component;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class WeixinPayService {

    private static Map<String, WxPayService> mpServices = Maps.newHashMap();


    public WxPayService getPayService(String payAppId, String payMchId, String payMchKey, String keyPath, String subMchId) {

        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(payMchId);
        payConfig.setMchId(payAppId);
        payConfig.setMchKey(payMchKey);
        payConfig.setKeyPath(keyPath);

        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);

        if (subMchId != null) {
            payConfig.setSubMchId(subMchId);
        }

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

}
